<?php
/**
 * Created by PhpStorm.
 * User: Mars.Li
 * Date: 2015/10/23
 * Time: 17:37
 */
namespace Admin\Controller;
class ReportsConfigController extends CommonController {


    function index() {
        //列表过滤器，生成查询Map对象
        $map = array();
        $model = D ('reports');
        if (! empty ( $model )) {
            //$this->_list ( $model, $map );
            $model = $model->where($map)->select();
            $this->assign ( 'lists', $model );
        }

        $this->display ();
        return;
    }

    function insert() {

        $model = D ('reports');

        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }
        //保存当前数据对象
        $list=$model->add ();
        if ($list!==false) { //保存成功
            $this->success ('新增成功!');
        } else {
            //失败提示
            $this->error ('新增失败!');
        }
    }

    function edit() {

        $model = D ( 'reports' );
        $id = $_REQUEST [$model->getPk ()];
        $vo = $model->getById ( $id );
        echo json_encode($vo);
    }

    function update() {

        $model = D ( 'reports' );
        if (false === $model->create ()) {
            $this->error ( $model->getError () );
        }

        // 更新数据
        $list=$model->save ();
        if (false !== $list) {
            //成功提示
            $this->success ('编辑成功!');
        } else {
            //错误提示
            $this->error ('编辑失败!');
        }
    }

    public function user()
    {
        //读取系统的用户列表
        $user    =   D("User");
        $list2=$user->field('id,account,nickname')->select();
        foreach ($list2 as $vo){
            $userList[$vo['id']]	=	$vo['account'].' '.$vo['nickname'];
        }

        $group    =   D("Reports");
        $list=$group->field('id,report_title')->select();
        foreach ($list as $vo){
            $groupList[$vo['id']]	=	$vo['report_title'];
        }
        $this->assign("groupList",$groupList);

        //获取当前用户组信息
        $groupId =  isset($_GET['id'])?$_GET['id']:'';
        $groupUserList = array();
        if(!empty($groupId)) {
            $this->assign("selectGroupId",$groupId);
            //获取当前组的用户列表
            $list	=	$group->getReportUserList($groupId);
            foreach ($list as $vo){
                $groupUserList[$vo['id']]	=	$vo['id'];
            }

        }
        $this->assign('groupUserList',$groupUserList);
        $this->assign('userList',$userList);
        $this->display();

        return;
    }

    /**
     * 授权
     */
    public function setUser()
    {
        $id     = $_POST['groupUserId'];
        $groupId	=	$_POST['groupId'];
        $group    =   D("Reports");
        $group->delReportUser($groupId);
        $result = $group->setReportUsers($groupId,$id);
        if($result===false) {
            $this->error('授权失败！');
        }else {
            $this->success('授权成功！');
        }
    }

    /**
     *  导入配置报表
     */
    public function installProfitReport(){

        Vendor('U8.Api');
        $u8 = new \U8Api("MSDB_REPORT_CONFIG");
        $u8->p_name='Proc_getU8codeWF';
        $model = D('profit_report');
        $result = $model->execute('TRUNCATE TABLE  profit_report');
        if (!$result) echo $model->getDbError();
        $month = date('m',time());
        for ($i = 1; $i<=$month-1; $i++) {
            $i < 10? $i='0'.$i:$i=$i;
            $parName['@StarDate'] = date('Y',time()).$i.'01';
            $parName['@EndDate']  = date('Y',time()).$i.'31';
            $list = $u8->callProcedur($parName);
            $year = date('Y',time());
            foreach ($list as $item) {
                $item['month'] = $i;
                $item['year'] = $year;
                $item['CitemName'] = iconv('GB2312','UTF-8',$item['CitemName']);
                $item['Dw_Name'] = iconv('GB2312','UTF-8',$item['Dw_Name']);
                if ($model->add($item) === false){
                    echo 'error'.$model->getLastSql();die;
                }
            }
        }
        $this->success( '同步成功！');
    }


}
